using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

namespace XtractorCSharp
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button buttonExtractText;
		private System.Windows.Forms.TextBox textBoxExtraction;
		private System.Windows.Forms.TextBox textBoxText2Extract;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

        [DllImport(@".\extr_api.dll")]
		public static extern int ExtrCreateDocumentMemory(ref IntPtr DocumentMemory);
		// int ExtrCreateDocumentMemory(void **DocumentMemory)


        [DllImport(@".\extr_api.dll")]

		public static extern int ExtrActivateHighlights(IntPtr DocumentMemory);

		// int ExtrActivateHighlights(void *DocumentMemory);

        [DllImport(@".\extr_api.dll")]
        public static extern int ExtrSetNumberPhrases(IntPtr DocumentMemory, double DesiredNumber);


        [DllImport(@".\extr_api.dll")]

		public static extern int ExtrGetHighlightByIndex(IntPtr DocumentMemory, int PhraseIndex, out IntPtr Highlight);

		//int ExtrGetHighlightByIndex(void *DocumentMemory, int HighlightIndex, void **Highlight);



        [DllImport(@".\extr_api.dll")]

		public static extern int ExtrCreateStopMemory(ref IntPtr StopMemory);

		// int ExtrCreateStopMemory(void **StopMemory);



        [DllImport(@".\extr_api.dll")]

		public static extern int ExtrSetInputCode(IntPtr DocumentMemory, int CharCodeID);

		// int ExtrSetInputCode(void *DocumentMemory, int CharCodeID);



        [DllImport(@".\extr_api.dll")]

		public static extern int ExtrReadDocumentBuffer(IntPtr DocumentMemory, IntPtr StopMemory, IntPtr DocumentBuffer, int BufferLength);

		// int ExtrReadDocumentBuffer(void *DocumentMemory, void *StopMemory, void *DocumentBuffer, int BufferLength);



        [DllImport(@".\extr_api.dll")]

		public static extern int ExtrSignalDocumentEnd(IntPtr DocumentMemory, IntPtr StopMemory);

		// int ExtrSignalDocumentEnd(void *DocumentMemory, void *StopMemory);



        [DllImport(@".\extr_api.dll")]

		public static extern int ExtrGetPhraseListSize(IntPtr DocumentMemory, ref int PhraseListSize);

		// int ExtrGetPhraseListSize(void *DocumentMemory, int *PhraseListSize);

 

		[DllImport(@".\extr_api.dll")]

		public static extern int ExtrGetPhraseByIndex(IntPtr DocumentMemory, int PhraseIndex, out IntPtr Phrase);

		// int ExtrGetPhraseByIndex(void *DocumentMemory, int PhraseIndex, void **Phrase);

 

		[DllImport(@".\extr_api.dll")]

		public static extern int ExtrClearStopMemory(IntPtr StopMemory);

		// int ExtrClearStopMemory(void *StopMemory);

            

		[DllImport(@".\extr_api.dll")]

		public static extern int ExtrClearDocumentMemory(IntPtr DocumentMemory);

		// int ExtrClearDocumentMemory(void *DocumentMemory);


		[DllImport(@".\extr_api.dll")]

		public static extern void ExtrGetErrorMessage(int ErrorCode, ref string ErrorMessage);

		// void ExtrGetErrorMessage(int ErrorCode, char **ErrorMessage);

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.buttonExtractText = new System.Windows.Forms.Button();
			this.textBoxExtraction = new System.Windows.Forms.TextBox();
			this.textBoxText2Extract = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// buttonExtractText
			// 
			this.buttonExtractText.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.buttonExtractText.Location = new System.Drawing.Point(336, 247);
			this.buttonExtractText.Name = "buttonExtractText";
			this.buttonExtractText.TabIndex = 5;
			this.buttonExtractText.Text = "Extract";
			this.buttonExtractText.Click += new System.EventHandler(this.button1_Click);
			// 
			// textBoxExtraction
			// 
			this.textBoxExtraction.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
			this.textBoxExtraction.AutoSize = false;
			this.textBoxExtraction.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.textBoxExtraction.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(0)), ((System.Byte)(192)));
			this.textBoxExtraction.Location = new System.Drawing.Point(0, 287);
			this.textBoxExtraction.Multiline = true;
			this.textBoxExtraction.Name = "textBoxExtraction";
			this.textBoxExtraction.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
			this.textBoxExtraction.Size = new System.Drawing.Size(742, 230);
			this.textBoxExtraction.TabIndex = 4;
			this.textBoxExtraction.Text = "Extraction ...";
			// 
			// textBoxText2Extract
			// 
			this.textBoxText2Extract.AutoSize = false;
			this.textBoxText2Extract.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.textBoxText2Extract.Location = new System.Drawing.Point(0, -1);
			this.textBoxText2Extract.Multiline = true;
			this.textBoxText2Extract.Name = "textBoxText2Extract";
			this.textBoxText2Extract.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
			this.textBoxText2Extract.Size = new System.Drawing.Size(742, 230);
			this.textBoxText2Extract.TabIndex = 3;
			this.textBoxText2Extract.Text = "Paste Text to be Extracted Here ...";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(7, 16);
			this.BackColor = System.Drawing.Color.White;
			this.ClientSize = new System.Drawing.Size(742, 516);
			this.Controls.Add(this.buttonExtractText);
			this.Controls.Add(this.textBoxExtraction);
			this.Controls.Add(this.textBoxText2Extract);
			this.Font = new System.Drawing.Font("Verdana", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Name = "Form1";
			this.Text = "DBI Technologies Inc. - Extractor 7.2 C# Demo";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			string summary = "";
			string text2Extract = this.textBoxText2Extract.Text;                

			int ErrorCode;
				
			//Create StopMemory used to hold the Stop Words (words not to be considered by extractor when performing the extraction)

			IntPtr StopMemory = new IntPtr(0);

			ErrorCode = ExtrCreateStopMemory(ref StopMemory);


			//Create Document Memory

			IntPtr DocumentMemory = new IntPtr(0);

			ErrorCode = ExtrCreateDocumentMemory(ref DocumentMemory);


			//Activate the Highlights functionality (returns a sentence with the extracted word)

			ErrorCode = ExtrActivateHighlights(DocumentMemory);

            ErrorCode = ExtrSetNumberPhrases(DocumentMemory, 30);
			//Read the document into memory 

			IntPtr str = Marshal.StringToHGlobalAnsi(text2Extract);

			ErrorCode = ExtrReadDocumentBuffer(DocumentMemory, StopMemory, str, text2Extract.Length);

			
			//Indicate to extractor this is all of the document

			ErrorCode = ExtrSignalDocumentEnd(DocumentMemory, StopMemory);
            string str2 = "";
            ExtrGetErrorMessage(ErrorCode, ref str2);

			//Process the extraction

			int PhraseListSize = 0;

			ErrorCode = ExtrGetPhraseListSize(DocumentMemory, ref PhraseListSize);

			for (int i = 0; i < PhraseListSize; i++)

			{

				IntPtr Phrase;

				IntPtr Highlight;

				//Get the keyphrase from the extractor engine

				ErrorCode = ExtrGetPhraseByIndex(DocumentMemory, i, out Phrase);

				summary += Marshal.PtrToStringAnsi(Phrase).ToUpper() + "\r\n";


				//Get the highlight associated with the key phrase

				ErrorCode = ExtrGetHighlightByIndex(DocumentMemory, i, out Highlight);

				summary += Marshal.PtrToStringAnsi(Highlight) + "\r\n" + "\r\n" ;

			}


			//Clear the memory

			ErrorCode = ExtrClearStopMemory(StopMemory);

			ErrorCode = ExtrClearDocumentMemory(DocumentMemory);

			this.textBoxExtraction.Text = summary;
		
		}
	}
}
